// package-activity/pages/activity-edit-delivery/index.js
const app = getApp();
import { navBack, tipToast } from '../../../common/env';
import { formatPrice } from '../../../common/utils';

Page({
    data: {
        activityInfo: {},
        unifiedShippingFee: '',
    },
    onLoad(options) {
        app.event.addListener('sendActivityInfoDelivery', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendActivityInfoDelivery');
    },
    _handleSendActivity(activityInfo) {
        const unifiedShippingFee = formatPrice(activityInfo.unifiedShippingFee, true);
        this.setData({
            activityInfo,
            unifiedShippingFee
        });
    },
    handleSwitchChange(e) {
        this.setData({
            'activityInfo.isUseWarehouseShippingFeeRule': e.detail
        });
    },
    handleChangeShippingFee(e) {
        const price = formatPrice(e.detail);
        const unifiedShippingFee = formatPrice(price, true);
        this.setData({
            unifiedShippingFee,
            'activityInfo.unifiedShippingFee': price,
        });
    },
    handleSave() {
        const { activityInfo } = this.data;
        const { isUseWarehouseShippingFeeRule, unifiedShippingFee } = activityInfo;
        if (!isUseWarehouseShippingFeeRule && (unifiedShippingFee === undefined || unifiedShippingFee === null)) {
            tipToast('请设置配送费');
            return;
        }
        app.event.trigger('sendActivityInfo', activityInfo);
        navBack();
    },
})
